﻿////////////////////////////////////////////////////////////////////////
//// Objects
////////////////////////////////////////////////////////////////////////

var METAL_TAB_ID = "metal_tab";
var metalAbbrev = new Array("au", "ag", "pt", "pd");

// The object for metals XML content
function MetalData() {
	this.date;
	this.code;
	this.value;
	this.ago;

	this.getTableRow = function(i) {
		var row = "<tr " + (((i+1) % 2 == 0) ? "class='even'>" : "class='odd'>");
		row += "<td><a href='#' title='"+prefs.getMsg("dynamic")+":&nbsp;" + prefs.getMsg(metalAbbrev[i]) + "' onclick='metalReportLink_OnClick(\""+this.code+"\",\""+this.date+"\",this.title); return false;'>";
		row += "<img src='"+_IG_GetImageUrl("http://www.cbr.ru/images/icon_"+metalAbbrev[i]+".gif")+"' width='14' height='11'><b>" + prefs.getMsg(metalAbbrev[i]) + "</b></a></td>";
		row += "<td align='center'>" + this.date.substr(0,5) + "</td>";
		row += "<td align='right'><b>" + formatValueToRegStd(normalizeFloat(this.value, 2)) + "</b></td>";
		var difference = this.value - this.ago;
		var diff_value = formatValueToRegStd(normalizeFloat(difference, 2));
		if (difference > 0) diff_value = "+" + diff_value;
		row += "<td align='right' class=' " + ((difference < 0) ? "decrease" : "increase") + "'>" + diff_value + "</td></tr>";

		return row;
	};
}


////////////////////////////////////////////////////////////////////////
//// Functions
////////////////////////////////////////////////////////////////////////

// Main function
function displayMetalInfo() {
	showLoadingInfo(METAL_TAB_ID);
	date = formatDateForCbrf(new Date());
	var url = "http://www.cbr.ru/scripts/xml_metall.asp?date_req1=" + formatDateForCbrf(getDateForNDaysAgo(date, 6)) + "&date_req2=" + date;
	_IG_FetchXmlContent(url, getMetalsDates);
}

//Function that find two last dates of the available values
function getMetalsDates(response) {
	if (responseError(response, METAL_TAB_ID)) {
		return;
	}

	var parentOfItemList = response.getElementsByTagName("Record");
	var lastMetalDate = parentOfItemList.item(parentOfItemList.length-1).getAttribute("Date");
	var preLastMetalDate = parentOfItemList.item(parentOfItemList.length-5).getAttribute("Date");
	var url = "http://www.cbr.ru/scripts/xml_metall.asp?date_req1=" + preLastMetalDate + "&date_req2=" + lastMetalDate;
	_IG_FetchXmlContent(url, _IG_Callback(parseMetalsData, lastMetalDate, preLastMetalDate));
}

// Function that parsing metals's database (XML)
function parseMetalsData(response, lastMetalDate, preLastMetalDate) {
	if (responseError(response, METAL_TAB_ID)) {
		return;
	}

	var metals = new Array();
	for (var i = 0; i < 4; i++) {
		metals[i] = new MetalData();
		metals[i].date = lastMetalDate;
		metals[i].code = i+1;
		var itemList = response.getElementsByTagName("Record");
		var nodeList = itemList.item(i).childNodes;
		for (var j = 0; j < nodeList.length; j++) {
			var node = nodeList.item(j);
			if (node.nodeName == "Buy") {
				metals[i].ago = parseFloat(replaceCommaWithDot(node.firstChild.nodeValue));
			}
		}
		var nodeList = itemList.item(i+4).childNodes;
		for (var j = 0; j < nodeList.length; j++) {
			var node = nodeList.item(j);
			if (node.nodeName == "Buy") {
				metals[i].value = parseFloat(replaceCommaWithDot(node.firstChild.nodeValue));
			}
		}
	}


	displayMetalData(metals);

	_IG_AdjustIFrameHeight();
}

// Function that displays main block of metals
function displayMetalData(metals) {
	var metalTable = buildMetalTable(metals);
	var copyright = "<div class='copyright'>" + 
										prefs.getMsg("copyright") + " <a href='http://www.cbr.ru' target='_blank'>http://www.cbr.ru</a>" + 
									"</div>";
	
	_gel(METAL_TAB_ID).innerHTML = metalTable + copyright;
}

// Function that building a table of metals
function buildMetalTable(metals) {
	var table = "<table class='rate_table'><thead>" + getMetalTableHeader() + "</thead><tbody>";
	for (var i = 0; i < metals.length; i++) {
		table += metals[i].getTableRow(i);
	}
	table += "</tbody></table>";
	return table;
}

// creating the header of output table
function getMetalTableHeader() {
	return "<tr>" +
				"<td>" + prefs.getMsg("metals") + "</td>" +
				"<td>" + prefs.getMsg("date") + "</td>" +
				"<td>" + prefs.getMsg("rub_per_gram") + "</td>" +
				"<td>" + prefs.getMsg("change") + "</td>" +
		   "</tr>";
}

//Function that handles onclick event of initialize pop-up window link
function metalReportLink_OnClick(code, rateDate, title) {
	var dateFrom = formatDateForCbrf(getDateForNDaysAgo(rateDate, RATE_PERIOD));
	var dateTo = formatDateForCbrf(rateDate);
	var url = "http://www.cbr.ru/scripts/xml_metall.asp?date_req1=" + dateFrom + "&date_req2=" + dateTo;
	_IG_FetchXmlContent(url, _IG_Callback(parseMetalDynamic, code, title));
}

// Function that parsing metals dynamic (XML)
function parseMetalDynamic(response, code, title) {
	if (responseError(response, METAL_TAB_ID)) {
		return;
	}

	var metalDynamic = new Array();

	// Get a list of the <Record> element nodes for given metal
	var itemList = response.getElementsByTagName("Record");
	var k = 0;
	for (var i = code-1; i < itemList.length; i += 4) {
		metalDynamic[k] = new RateDynamic();
		metalDynamic[k].date = itemList.item(i).getAttribute("Date");
		var nodeList = itemList.item(i).childNodes;
		for (var j = 0; j < nodeList.length; j++) {
			var node = nodeList.item(j);
			if (node.nodeName == "Buy") {
			    metalDynamic[k].value = normalizeFloat(replaceCommaWithDot(node.firstChild.nodeValue), 2);
			}
		}
		k++;
	}
	// This function will initialize pop-up window, create table and graph when we retreive metal dynamic from XML
	CreateReportWindow(metalDynamic, title);
}